<?php

/**
 * This is the model class for table "{{video}}".
 *
 * The followings are the available columns in table '{{video}}':
 * @property integer $id
 * @property string $title
 * @property string $youtube
 * @property integer $file_id
 * @property string $img
 * @property string $body
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $status
 * @property integer $is_trade
 * @property integer $user_id
 * @property integer $cat_id
 * @property integer $view
 *
 * The followings are the available model relations:
 * @property Comments[] $comments
 * @property Files $file
 * @property User $user
 * @property VideoCat $cat
 */
class Video extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Video the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{video}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('title', 'required'),
			array('file_id,view, create_time, update_time, status, is_trade, user_id, cat_id', 'numerical', 'integerOnly'=>true),
			array('title,img, youtube', 'length', 'max'=>255),
			array('body', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, title, youtube, file_id, body, create_time, update_time, status, is_trade, user_id, cat_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'comments' => array(self::HAS_MANY, 'Comments', 'item_id','condition'=>'status=1 AND item_type="video"'),
			'commentCount' => array(self::STAT, 'Comments', 'item_id','condition'=>'status=1 AND item_type="video"'),
			'rate' => array(self::HAS_MANY, 'VideoRate', 'video_id'),
			'rateCount' => array(self::STAT, 'VideoRate', 'video_id'),
			'ownrateCount' => array(self::STAT, 'VideoRate', 'video_id','condition'=>'user_id='.Yii::app()->user->id),
			'file' => array(self::BELONGS_TO, 'Files', 'file_id'),
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'cat' => array(self::BELONGS_TO, 'VideoCat', 'cat_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'title' => 'Tiêu đề',
			'youtube' => 'Link youtube',
			'file_id' => 'File video',
			'body' => 'Nội dung',
			'create_time' => 'Thời gian đăng',
			'update_time' => 'Thời gian cập nhật',
			'status' => 'Trạng thái',
			'is_trade' => 'Tính phí',
			'user_id' => 'Thành viên',
			'cat_id' => 'Danh mục',
			'view' => 'Lượt xem',
			'img' => 'Ảnh',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('youtube',$this->youtube,true);
		$criteria->compare('file_id',$this->file_id);
		$criteria->compare('body',$this->body,true);
		$criteria->compare('create_time',$this->create_time);
		$criteria->compare('update_time',$this->update_time);
		$criteria->compare('status',$this->status);
		$criteria->compare('is_trade',$this->is_trade);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('cat_id',$this->cat_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public function beforeSave()
    {

        if ($this->isNewRecord) {
            $this->create_time = time();
            $this->update_time = time();
            if(!Yii::app()->user->isGuest){
                $this->user_id = Yii::app()->user->id;
            }
        } else {
            $this->update_time = time();
        }
        return parent::beforeSave();
    }

    function getStar(){
        if($this->rateCount > 0){

            return $this->averrate/2;
        }
        return 1;
    }
    function getAverrate(){
        if($this->rateCount > 0){
            $total = 0;
            foreach($this->rate as $rate){
                $total += $rate->marks;
            }
            return round($total/$this->rateCount,2);
        }
        return 0;
    }

    /**
     * @param $comment Comments
     * @return mixed
     */
    public function addComment($comment)
    {
        if(Yii::app()->params['commentNeedApproval'])
            $comment->status=2;
        else
            $comment->status=1;
        $comment->item_type = 'video';
        $comment->item_id=(int)$this->id;
        return $comment->save();
    }
    public function getUrl()
    {
        return Yii::app()->createUrl('video/view', array(
            'vid'=>$this->id,
            'title'=>Yii::app()->func->safeTitle($this->title),
        ));
    }
}